home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / Level 0 Macintosh 29Sep94 / Scrap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  3.9 KB  |  129 lines  |  [TEXT/KAHL]

  1. /* Scrap.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    System Dependency Library for Building Portable Software               */
  5. /*    Macintosh Version                                                      */
  6. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  7. /*                                                                           */
  8. /*    This file is Public Domain; it may be used for any purpose whatsoever  */
  9. /*    without restriction.                                                   */
  10. /*                                                                           */
  11. /*    This package is distributed in the hope that it will be useful,        */
  12. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  13. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  14. /*                                                                           */
  15. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18.  
  19. #include "MiscInfo.h"
  20. #include "Debug.h"
  21. #include "Audit.h"
  22. #include "Definitions.h"
  23.  
  24. #ifdef THINK_C
  25.     #pragma options(pack_enums)
  26. #endif
  27. #include <Scrap.h>
  28. #include <Memory.h>
  29. #include <Errors.h>
  30. #ifdef THINK_C
  31.     #pragma options(!pack_enums)
  32. #endif
  33.  
  34. #include "Scrap.h"
  35. #include "Memory.h"
  36.  
  37.  
  38. #define ScrapFlushThreshHold (4096L) /* max size that's kept in memory */
  39.  
  40.  
  41. EXECUTE(static MyBoolean                    ScrapInitialized = False;)
  42.  
  43.  
  44. /* initialize scrap handler by zeroing the scrap */
  45. MyBoolean        Eep_InitializeScrapHandler(void)
  46.     {
  47.         ERROR(ScrapInitialized,PRERR(ForceAbort,
  48.             "InitializeScrapHandler called multiple times"));
  49.         EXECUTE(ScrapInitialized = True;)
  50.         if (InfoScrap()->scrapState < 0)
  51.             {
  52.                 ZeroScrap(); /* initialize (probably not needed under multifinder) */
  53.             }
  54.         return True;
  55.     }
  56.  
  57.  
  58. /* shut down the scrap handler.  this doesn't do anything right now */
  59. void                Eep_ShutdownScrapHandler(void)
  60.     {
  61.         ERROR(!ScrapInitialized,PRERR(ForceAbort,"Scrap handler not initialized"));
  62.     }
  63.  
  64.  
  65. /* get a block containing a copy of the scrap */
  66. /* if a block couldn't be allocated, then it returns NIL */
  67. char*                GetCopyOfScrap(void)
  68.     {
  69.         char*                TheData;
  70.         long                Offset;
  71.         char**            TheHandle;
  72.         long                ScrapLength;
  73.  
  74.         ERROR(!ScrapInitialized,PRERR(ForceAbort,"Scrap handler not initialized"));
  75.  
  76.         /* find out if the scrap has a TEXT on it */
  77.         ScrapLength = GetScrap(NIL,'TEXT',&Offset); /* we can only handle text */
  78.         if (ScrapLength < 0)
  79.             {
  80.                 return NIL; /* no scrap */
  81.             }
  82.  
  83.         /* allocate a place to put the scrap */
  84.         TheHandle = NewHandle(ScrapLength);
  85.         if (TheHandle == NIL)
  86.             {
  87.                 OSErr            Error;
  88.  
  89.                 TheHandle = TempNewHandle(ScrapLength,&Error);
  90.             }
  91.         if (TheHandle == NIL)
  92.             {
  93.                 return NIL;
  94.             }
  95.  
  96.         /* save the scrap in a local heap block for them to use */
  97.         GetScrap(TheHandle,'TEXT',&Offset);
  98.         TheData = AllocPtrCanFail(GetHandleSize(TheHandle),"Scrap");
  99.         if (TheData != NIL)
  100.             {
  101.                 CopyData(*TheHandle,TheData,GetHandleSize(TheHandle));
  102.             }
  103.         DisposeHandle(TheHandle);
  104.  
  105.         return TheData;
  106.     }
  107.  
  108.  
  109. /* make a copy of the block and put the data into the scrap */
  110. /* returns True if successful */
  111. MyBoolean        SetScrapToThis(char* DataToCopy)
  112.     {
  113.         long            ScrapSize;
  114.         EXECUTE(long ErrorCode;)
  115.  
  116.         ERROR(!ScrapInitialized,PRERR(ForceAbort,"Scrap handler not initialized"));
  117.         CheckPtrExistence(DataToCopy);
  118.         ScrapSize = PtrSize(DataToCopy);
  119.         ZeroScrap();
  120.         EXECUTE(ErrorCode = ) PutScrap(ScrapSize,'TEXT',&(DataToCopy[0]));
  121.         ERROR(ErrorCode != noErr,PRERR(AllowResume,"SetScrapToThis:  non-zero error code"));
  122.         if (ScrapSize >= ScrapFlushThreshHold)
  123.             {
  124.                 UnloadScrap();
  125.             }
  126.         /* how can we tell if it failed? */
  127.         return True;
  128.     }
  129.